<template>
	<div class="bodys  u-flex">
		<div class="header">
			<div>
				<image :src="userimg" mode=""></image>
				
			</div>
			
		</div>
		<div class="center">
			<div>
				<h3>请授权以下信息来建立合作关系</h3>
			</div>
			<div>
				<ul>
					<li>
						<p>获取您的部分信息，头像，名称</p>
					</li>
				</ul>
			</div>
		</div>
		<!-- <div>
			<button type="default" @click="huancun()">缓存</button>
			<button type="default" @click="gethuancun()">获取缓存</button>
		</div> -->
		<div class="shouquan">
			<!-- <button type="default" @click="goto()">授权</button> -->
			<button open-type="getUserInfo" hover-class='none' @getuserinfo="getUserInfoFun">授权的</button>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				took: false,
				username:"",
				userimg:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596778438019&di=c0a6bbb64f966ccc4fc8df8a883ec830&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fc628cc36fdb6234a429ccdc2330066958e2c4af558a5f-MUTOC5_fw658"
			}
		},
		onLoad() {
			console.log("载入中")
		},
		methods: {
			goto(x) {
				var that=this
				console.log("载入了发送程序")
				console.log("我的微信code"+x.code)
				try {
				  wx.setStorageSync('nickName',x.nickName)
				  wx.setStorageSync('avatarUrl',x.avatarUrl)
				} catch (e) {
					console.log(e)
				}
				 wx.request({
				  url: this.$url+'/api/v1.Login/login', 
				  method:'post',
				  data: {
				    wx_nick_name: x.nickName,
					wx_total:x.avatarUrl,
					code:x.code
				  },
				  success(res){
					  console.log("返回的结果"+res.data.data.token)
					  wx.setStorage({
					    key:"token",
					    data:res.data.data.token
					  })
				    console.log("发送成功")
					console.log(res.data)
					if(!res.data.data.customer_id){
						console.log("我是业务员"+res.data.data.agent_id)
						wx.setStorage({
						  key:"agent_id",
						  data:res.data.data.agent_id
						})
						uni.navigateTo({
							url:"../shop/banlv"
						})
					}
					if(res.data.data.customer_id){
						uni.navigateTo({
							url:"../user/index"
						})
						console.log("我是客户"+res.data.data.customer_id)
						wx.setStorage({
						  key:"customer_id",
						  data:res.data.data.customer_id
						})
					}
					
				  },
				  fail(ref){
					  console.log("发送失败")
					  console.log(ref)
				  }
				})
			},
			getUserInfoFun() {
				var that=this
				wx.login({
					success: function(res) {
						var code =res.code
					wx.getUserInfo({
							success: function(res) {
								var userInfo = res.userInfo
								var nickName = userInfo.nickName
								var avatarUrl = userInfo.avatarUrl
								var gender = userInfo.gender //性别 0：未知、1：男、2：女
								var province = userInfo.province
								var city = userInfo.city
								var country = userInfo.country
								// console.log(res.userInfo)
								// console.log(nickName,avatarUrl)
								// this.username=nickName
								// this.userimg=avatarUrl
								// console.log("返回的信息"+code)
								// console.log(nickName)
								var userlist={
									nickName:nickName,
									avatarUrl:avatarUrl,
									code:code
								}
								// console.log(userlist)
								// console.log(code)
								that.goto(userlist)	
								
							}
						})
					}
				});
			},
		},
		beforeCreate() {}
	}
</script>

<style scoped lang="scss">
	.bodys {
		height: 100%;
		flex-direction: column;
		justify-content: space-around;

		.header image {
			// background-color: red;
			height: 300upx;
			width: 300upx;
			border-radius: 50%;
			margin: 0 auto;
		}

		.center {
			margin: 0 auto;

			// width: 100%;
			p {
				color: #D3D3D3
			}

			h3 {}

		}

		.shouquan {
			width: 50%;
			margin: 0 auto;
			height: 100upx;
			line-height: 300rpx;
			color: $f;
		}
	}
</style>
